package com.diandian.mobile;




import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.diandian.common.Constant;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

/**
 * @description MobileFilter
 * @author TDW
 * @date 2016年12月28日
 *
 */
public class MobileFilter extends ZuulFilter {

    //private static final Logger log = Logger.getLogger(MobileFilter.class);
	private static final Logger logger = LogManager.getLogger(MobileFilter.class.getName());
    /** 灰度环境mobile serverId */
    private static final String GRAY_DDMOBILE_SERVER_ID = "gray_dd_mobile";
    /** mobile正式环境serverId */
    private static final String DDMOBILE_SERVER_ID = "dd_mobile";
    public boolean shouldFilter() {
        return true;
    }


    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();

        //ctx.setRouteHost(new URL("http://localhost:8080”));

        String uri = ctx.getRequest().getRequestURI();
        String appId = uri.split("/")[1];
        String shopId = ctx.getRequest().getParameter("shop_id");
        if(StringUtils.isBlank(shopId)){
        	shopId = ctx.getRequest().getParameter("shopId");
        }
        if (appId.equalsIgnoreCase(Constant.mobile)) {
        	logger.info("#####request dd_mobile ："+uri);
            //ctx.setSendZuulResponse(false);
            if(StringUtils.isNotBlank(shopId) && Constant.grayshops.contains(shopId)){//灰度
                ctx.set("serviceId", GRAY_DDMOBILE_SERVER_ID);
            }else{//非灰度
                ctx.set("serviceId", DDMOBILE_SERVER_ID);
            }
        }
        return null;
    }


    public String filterType() {
        return "pre";
    }


    public int filterOrder() {
        return 1;
    }

}

